package fastjson.code;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;

import java.util.Date;

/**
 * Description: JSONField的示例demo
 *
 * @author code-worker
 * @date 2021/08/21 8:53
 */
public class JsonFieldDemo {
    //此处采用public修饰变量，以省略setter/getter
    @JSONField(name = "Name")
    private String name;
    @JSONField(format = "yyyyMMdd")
    private Date date;
    @JSONField(serialize = false)
    private String phone;
    @JSONField(jsonDirect = true)
    private String originFormat;
    @JSONField(alternateNames = {"other", "otherInfo"})
    private String otherInfo;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getOriginFormat() {
        return originFormat;
    }

    public void setOriginFormat(String originFormat) {
        this.originFormat = originFormat;
    }

    public String getOtherInfo() {
        return otherInfo;
    }

    public void setOtherInfo(String otherInfo) {
        this.otherInfo = otherInfo;
    }

    @Override
    public String toString() {
        return "JsonFieldDemo{" +
                "name='" + name + '\'' +
                ", date=" + date +
                ", phone='" + phone + '\'' +
                ", originFormat='" + originFormat + '\'' +
                ", otherInfo='" + otherInfo + '\'' +
                '}';
    }

    public static void main(String[] args) {
        JsonFieldDemo demo = new JsonFieldDemo();
        demo.name = "demo";
        demo.date = new Date();
        demo.phone = "13888888888";
        demo.originFormat = "{'originFormat': 'originFormat'}";
        demo.otherInfo = "other";
        System.out.println(JSON.toJSONString(demo));

        String str1 = "{\"Name\":\"demo\",\"date\":\"20210821\",\"phone\":\"13888888888\",\"other\":\"other\"}";
        System.out.println(JSON.parseObject(str1, JsonFieldDemo.class));
    }

}
